"use strict";
const { Model, Sequelize, Op } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
  class Opportunity extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      Opportunity.belongsTo(models.Staff, {
        foreignKey: "staff_id",
        allowNull: true,
      });
      Opportunity.belongsTo(models.Staff, {
        foreignKey: "checker_id",
        as: "checker", //审核者
        allowNull: true,
      });
      Opportunity.belongsTo(models.Client, {
        foreignKey: "client_id",
        allowNull: true,
      });
      // define association here
    }
  }
  Opportunity.init(
    {
      business: DataTypes.STRING, //商机名称，后面转换成项目的名字
      date_release: {
        type: DataTypes.DATE,
        defaultValue: Sequelize.NOW,
      },
      source: DataTypes.STRING,
      possibility: DataTypes.STRING,
      stage: DataTypes.STRING,
      value: DataTypes.STRING,
      status: {
        type: DataTypes.STRING,
        defaultValue: "待审核",
      },
      check_sug: DataTypes.STRING,
    },
    {
      sequelize,
      modelName: "Opportunity",
      tableName: "opportunities",
    }
  );
  return Opportunity;
};
